package com.example.gisapplication;

import android.graphics.Color;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PointCollection;
import com.esri.arcgisruntime.geometry.Polygon;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.ArcGISVectorTiledLayer;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.portal.Portal;
import com.esri.arcgisruntime.portal.PortalItem;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.UniqueValueRenderer;

import java.util.ArrayList;
import java.util.List;

public class GraphicsOverlayActivity extends AppCompatActivity {

    MapView mapView;


    private String sample_service_url = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_graphics_overlay);

        mapView = findViewById(R.id.mapView);

//        ArcGISRuntimeEnvironment.setApiKey("AAPK0ab150e1d9bc46068e843c65d3f4f9de3zFj504-cixyq2tOkn451NKj_3lzvVrM5-QY6-SZn2ulWmkW1Ec-gJSlaPpdWEqQ");

//        displayMap();
//        displayWebMap();
//        displayInnerMap();
//        display1();

//        drawPoint();
//        drawLine();
//        drawPolygon();
//        startActivity(new Intent(this, LayersActivity.class));
        UniqueValueRenderer();

    }

    private void display1() {
//        String url = "http://services.arcgisonline.com/arcgis/rest/services/World_Topo_Map/MapServer";
//
//        // create new Tiled Layer from service url
//        ArcGISTiledLayer arcGISTiledLayer = new ArcGISTiledLayer(url);
//        // set tiled layer as basemap
//        Basemap basemap = new Basemap(arcGISTiledLayer);
//        // create a map with the basemap
//        ArcGISMap arcGISMap = new ArcGISMap(basemap);
//        // set the map to be displayed in this view
//        mapView.setMap(arcGISMap);


//        1.传统GIS
//        对于一直使用ArcGIS Server发布地图服务的，可以直接使用地图服务（Layer）来加载在线的地图服务（ArcGIS Server）。

        String mid_century_url = "https://www.arcgis.com/home/item.html?id=7675d44bb1e4428aa2c30a9b68f97822";
        ArcGISVectorTiledLayer mVectorTiledLayer = new ArcGISVectorTiledLayer(mid_century_url);
        Basemap basemap = new Basemap(mVectorTiledLayer);
        ArcGISMap map = new ArcGISMap(basemap);
        Viewpoint vp = new Viewpoint(47.606726, -122.335564, 72223.819286);
        map.setInitialViewpoint(vp);
        mapView.setMap(map);


//        2.WebGIS（Online & Portal）
//        通过Online或者Portal可以便捷快速的制作出更简洁智能的地图资源，用以表达我们的目的，对于移动端而言这些Web Map可以直接应用。

//        String url = "http://www.arcgis.com/home/webmap/viewer.html?webmap=55c1665bcd064552944a9e8296271ec3";
//        ArcGISMap arcGISMap = new ArcGISMap(url);
//        mapView.setMap(arcGISMap);
//
//        Basemap basemap = arcGISMap.getBasemap(); //获取底图
//        LayerList operationalLayers = arcGISMap.getOperationalLayers(); //获取业务图层


//        3.在线矢量切片：ArcGISVectorTiledLayer

//        String url = "https://www.arcgis.com/home/item.html?id=e19e9330bf08490ca8353d76b5e2e658";
//        ArcGISVectorTiledLayer arcGISVectorTiledLayer = new ArcGISVectorTiledLayer(url);
//        Basemap basemap = new Basemap(arcGISVectorTiledLayer);
//        ArcGISMap arcGISMap = new ArcGISMap(basemap);
//        mapView.setMap(arcGISMap);
//        Viewpoint vp = new Viewpoint(47.606726, -122.335564, 72223.819286);
//        arcGISMap.setInitialViewpoint(vp);


    }

    private void displayInnerMap() {
        ArcGISMap arcGISMap1 = new ArcGISMap(Basemap.createStreets());
        ArcGISMap arcGISMap2 = new ArcGISMap(Basemap.createImagery());
        ArcGISMap arcGISMap3 = new ArcGISMap(Basemap.createStreetsVector());

        ArcGISMap arcGISMap4 = new ArcGISMap(Basemap.createTopographic());

        //初始化可见区域
        Envelope targetExtent = new Envelope(-13639984.0, 4537387.0, -13606734.0, 4558866.0,
                SpatialReferences.getWebMercator());
        Viewpoint initViewpoint = new Viewpoint(targetExtent);
        arcGISMap4.setInitialViewpoint(initViewpoint);

        mapView.setMap(arcGISMap4);
    }


    private void displayMap() {
        ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
        mapView.setMap(map);
        mapView.setViewpoint(new Viewpoint(34.0270, -118.8050, 72000.0));
    }

    private void displayWebMap() {
        Portal portal = new Portal("https://www.arcgis.com", false);

        String itemId = "41281c51f9de45edaf1c8ed44bb10e30";
        PortalItem portalItem = new PortalItem(portal, itemId);
        ArcGISMap map = new ArcGISMap(portalItem);
        mapView.setMap(map);
//        mapView.setViewpoint(new Viewpoint(34.0270, -118.8050, 72000.0));
    }


    //添加点
    private void drawPoint() {
        GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
        mapView.getGraphicsOverlays().add(graphicsOverlay);

        Point point = new Point(-118.8065, 34.0005, SpatialReferences.getWgs84());

        // create an opaque orange (0xFFFF5733) point symbol with a blue (0xFF0063FF) outline symbol
        SimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, -0xa8cd, 10f);

        SimpleLineSymbol blueOutlineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, -0xff9c01, 2f);
        simpleMarkerSymbol.setOutline(blueOutlineSymbol);

        Graphic pointGraphic = new Graphic(point, simpleMarkerSymbol);

        graphicsOverlay.getGraphics().add(pointGraphic);
    }

    private void drawLine() {

        GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
        mapView.getGraphicsOverlays().add(graphicsOverlay);

        PointCollection polylinePoints = new PointCollection(SpatialReferences.getWgs84());
        polylinePoints.add(new Point(-118.8215, 34.0139));
        polylinePoints.add(new Point(-118.8148, 34.0080));
        polylinePoints.add(new Point(-118.8088, 34.0016));
        Polyline polyline = new Polyline(polylinePoints);

        SimpleLineSymbol polylineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, -0xff9c01, 3f);

        Graphic polylineGraphic = new Graphic(polyline, polylineSymbol);

        // add the polyline graphic to the graphics overlay
        graphicsOverlay.getGraphics().add(polylineGraphic);

    }

    private void drawPolygon() {
        GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
        mapView.getGraphicsOverlays().add(graphicsOverlay);

        PointCollection polygonPoints = new PointCollection(SpatialReferences.getWgs84());

        polygonPoints.add(new Point(-118.8189, 34.0137));
        polygonPoints.add(new Point(-118.8067, 34.0215));
        polygonPoints.add(new Point(-118.7914, 34.0163));
        polygonPoints.add(new Point(-118.7959, 34.0085));
        polygonPoints.add(new Point(-118.8085, 34.0035));

        // create a polygon geometry from the point collection
        Polygon polygon = new Polygon(polygonPoints);

        // create an opaque orange (0xFFFF5733) point symbol with a blue (0xFF0063FF) outline symbol
        SimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, -0xa8cd, 10f);

        SimpleLineSymbol blueOutlineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, -0xff9c01, 2f);
        simpleMarkerSymbol.setOutline(blueOutlineSymbol);

        // create an orange fill symbol with 20% transparency and the blue simple line symbol
        SimpleFillSymbol polygonFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, -0x7f00a8cd, blueOutlineSymbol);

        Graphic polygonGraphic = new Graphic(polygon, polygonFillSymbol);
        // add the polygon graphic to the graphics overlay
        graphicsOverlay.getGraphics().add(polygonGraphic);
    }


//    UniqueValueRenderer表示具有匹配属性的功能/图形组。这在名义或字符串数据中最常见。
    private void UniqueValueRenderer() {
        ArcGISMap map = new ArcGISMap(Basemap.createTopographic());
        ServiceFeatureTable serviceFeatureTable = new ServiceFeatureTable(sample_service_url);
        FeatureLayer featureLayer = new FeatureLayer(serviceFeatureTable);

        UniqueValueRenderer uniqueValueRenderer = new UniqueValueRenderer();
        // Set the field to use for the unique values
        //You can add multiple fields to be used for the renderer in the form of a list, in this case
        uniqueValueRenderer.getFieldNames().add("STATE_ABBR");
        // we are only adding a single field

        // Create the symbols to be used in the renderer
        SimpleFillSymbol defaultFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.NULL, Color.BLACK,
                new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.GRAY, 2));

        SimpleFillSymbol californiaFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.RED,
                new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.RED, 2));

        SimpleFillSymbol arizonaFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.GREEN,
                new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.GREEN, 2));


        SimpleFillSymbol nevadaFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.BLUE,
                new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 2));

        uniqueValueRenderer.setDefaultSymbol(defaultFillSymbol);
        uniqueValueRenderer.setDefaultLabel("Other");


        // Set value for california
        List<Object> californiaValue = new ArrayList<>();
        // You add values associated with fields set on the unique value renderer.
        // If there are multiple values, they should be set in the same order as the fields are set
        californiaValue.add("CA");
        uniqueValueRenderer.getUniqueValues().add(new UniqueValueRenderer.UniqueValue("California", "State of California", californiaFillSymbol,
                californiaValue));


        // Set value for arizona
        List<Object> arizonaValue = new ArrayList<>();
        // You add values associated with fields set on the unique value renderer.
        // If there are multiple values, they should be set in the same order as the fields are set
        arizonaValue.add("AZ");
        uniqueValueRenderer.getUniqueValues()
                .add(new UniqueValueRenderer.UniqueValue("Arizona", "State of Arizona", arizonaFillSymbol, arizonaValue));


        // Set value for nevada
        List<Object> nevadaValue = new ArrayList<>();
        // You add values associated with fields set on the unique value renderer.
        // If there are multiple values, they should be set in the same order as the fields are set
        nevadaValue.add("NV");
        uniqueValueRenderer.getUniqueValues()
                .add(new UniqueValueRenderer.UniqueValue("Nevada", "State of Nevada", nevadaFillSymbol, nevadaValue));


        // Set the renderer on the feature layer
        featureLayer.setRenderer(uniqueValueRenderer);
        //[DocRef: END]

        // add the layer to the map
        map.getOperationalLayers().add(featureLayer);

        map.setInitialViewpoint(new Viewpoint(
                new Envelope(-13893029.0, 3573174.0, -12038972.0, 5309823.0, SpatialReferences.getWebMercator())));

        // set the map to be displayed in the mapview
        mapView.setMap(map);

    }


    @Override
    protected void onPause() {
        mapView.pause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mapView.resume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mapView.dispose();
        super.onDestroy();
    }

}